/****************************************************************************
  Copyright (C) 2010 Wanderson Regis Silva
  All rights reserved
  Contact: Wanderson Regis Silva (wanderson.regis@ymail.com)

  This file is part of OpenEditor project

  This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/

#include <qapplication.h>
#include <qcheckbox.h>
#include <qdialogbuttonbox.h>
#include <qformlayout.h>
#include <qpushbutton.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qlineedit.h>

#include "codeedit.h"
#include "finddialog.h"

FindDialog::FindDialog(QWidget *parent) : QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint), __target(0) {
  setModal(false);
  setWindowTitle(tr("%1 - Find and Replace").arg(qApp->applicationName()));

  __lineEdit = new QLineEdit;

  __replaceEdit = new QLineEdit;

  __caseCheckBox = new QCheckBox(tr("Match case"));
  __useRegularExpressionCheckBox = new QCheckBox(tr("Use regular expression"));

  QPushButton *findButton = new QPushButton(tr("Find Next"));
  findButton->setDefault(true);
  connect(findButton, SIGNAL(clicked()), this, SLOT(findAct()));

  QPushButton *findAllButton = new QPushButton(tr("Find All"));
  findAllButton->setAutoDefault(false);
  connect(findAllButton, SIGNAL(clicked()), this, SLOT(findAllAct()));

  QPushButton *replaceButton = new QPushButton(tr("Replace"));
  replaceButton->setAutoDefault(false);
  connect(replaceButton, SIGNAL(clicked()), this, SLOT(replaceAct()));

  QPushButton *replaceAllButton = new QPushButton(tr("Replace All"));
  replaceAllButton->setAutoDefault(false);
  connect(replaceAllButton, SIGNAL(clicked()), this, SLOT(replaceAllAct()));

  QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Vertical);
  buttonBox->addButton(findButton, QDialogButtonBox::ActionRole);
  buttonBox->addButton(findAllButton, QDialogButtonBox::ActionRole);
  buttonBox->addButton(replaceButton, QDialogButtonBox::ActionRole);
  buttonBox->addButton(replaceAllButton, QDialogButtonBox::ActionRole);

  QFormLayout *findLayout = new QFormLayout;
  findLayout->addRow(tr("Find:"), __lineEdit);
  findLayout->addRow(tr("Replace:"), __replaceEdit);

  QVBoxLayout *leftLayout = new QVBoxLayout;
  leftLayout->addLayout(findLayout);
  leftLayout->addWidget(__caseCheckBox);
  leftLayout->addWidget(__useRegularExpressionCheckBox);
  leftLayout->addStretch(1);

  QHBoxLayout *mainLayout = new QHBoxLayout(this);
  mainLayout->addLayout(leftLayout);
  mainLayout->addWidget(buttonBox);
}

void FindDialog::setTarget(QWidget *target) {
  __target = target;
}

void FindDialog::findAct(void) {
  CodeEdit *c = dynamic_cast<CodeEdit*>(__target);

  if(c) {
    QTextDocument::FindFlags caseSens = 0;

    if(__caseCheckBox->isChecked())
      caseSens = QTextDocument::FindCaseSensitively;

    c->find(__lineEdit->text(), caseSens);
  }
}

void FindDialog::findAllAct(void) {
}

void FindDialog::replaceAct(void) {
}

void FindDialog::replaceAllAct(void) {
}
